package project.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import project.dao.StudentDao;
import project.entity.Student;
import project.utils.DruidUtil;
import project.utils.Page;

import java.sql.SQLException;
import java.util.List;

/*
杨威
2021/9/11
17:18
*/
public class StudentDaoImpl implements StudentDao {
    QueryRunner queryRunner = new QueryRunner();

    @Override
    public List<Student> studentList() {
        try {
            String sql = "select * from student";
            return queryRunner.query(DruidUtil.getConnection(), sql, new BeanListHandler<>(Student.class));
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "查询所有学生出现异常");
        }

    }

    @Override
    public List<Student> selectAllByPage(Page page) {
        try {
            String sql = "select * from student limit ?,?";
            return queryRunner.query(DruidUtil.getConnection(), sql, new BeanListHandler<>(Student.class), page.getStartRow(), page.getPageSize());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "分页查询出现异常");
        }

    }

    @Override
    public Long selectCount() {
        try {
            String sql = "select count(*) from student";
            return queryRunner.query(DruidUtil.getConnection(), sql, new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "查询数量出现异常");
        }
    }

    @Override
    public int insert(Student student) {
        try {
            String sql = "insert into student(stuId,stuName,stuGender,stuPhone,bornDate,stuEmail,stuAddress,gradeId)values(?,?,?,?,?,?,?,?)";
            Object[] params = {0, student.getStuName(), student.getStuGender(), student.getStuPhone(), student.getBornDate(), student.getStuEmail(), student.getStuAddress(), student.getGradeId()};
            return queryRunner.update(DruidUtil.getConnection(), sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"新增学生出现异常");
        }
    }

    @Override
    public int update(Student student) {
        try {
            String sql="update student set stuName=?,stuGender=?,stuPhone=?,bornDate=?,stuEmail=?,stuAddress=?,gradeId=? where stuId=?";
            Object[] params={student.getStuName(),student.getStuGender(),student.getStuPhone(),student.getBornDate(),student.getStuEmail(),student.getStuAddress(),student.getGradeId(),student.getStuId()};
            return queryRunner.update(DruidUtil.getConnection(),sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"修改学生出现异常");
        }
    }

    @Override
    public int delete(int stuId) {
        try {
            String sql="delete from student where stuId=?";
            return queryRunner.update(DruidUtil.getConnection(),sql,stuId);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"删除学生出现异常");
        }
    }

    @Override
    public Student selectById(int stuId) {
        try {
            String sql="select * from student where stuId=?";
            return queryRunner.query(DruidUtil.getConnection(),sql,new BeanHandler<>(Student.class),stuId);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"根据ID查询学生出现异常");
        }
    }
}
